/**
 * 
 */
package com.gragra.wilsHandler;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
/**
 * @author Christoph Teichmann
 * 14.10.2013
 */
public class WilsToSimpleTaggedCorpora
{
	/**
	 * 
	 */
	private final static int	WordPostion = 1;
	/**
	 * 
	 */
	private final static int CoarsePosition = 3;
	/**
	 * 
	 */
	private final static int UniversalPosition = 5;
	/**
	 * 
	 */
	private static final int FinePosition = 4;
	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) throws IOException
	{
		String prefix = args[0];
		BufferedWriter fine = new BufferedWriter(new FileWriter(prefix+"_fine.txt"));
		BufferedWriter coarse = new BufferedWriter(new FileWriter(prefix+"_coarse.txt"));
		BufferedWriter uni = new BufferedWriter(new FileWriter(prefix+"_universal.txt"));
		boolean first = true;
		for(int i=1;i<args.length;++i)
		{
			BufferedReader input = new BufferedReader(new FileReader(args[i]));
			ArrayList<String> words = new ArrayList<String>();
			ArrayList<String> fT = new ArrayList<String>();
			ArrayList<String> cT = new ArrayList<String>();
			ArrayList<String> uT = new ArrayList<String>();
			String line;
			while((line = input.readLine()) != null)
			{
				line = line.trim();
				if(line.equals(""))
				{
					first = writeOut(fine, coarse, uni, first, words, fT, cT,uT);
					continue;
				}
				String[] parts = line.split("\\s+");
				words.add(parts[WordPostion]);
				fT.add(parts[FinePosition]);
				cT.add(parts[CoarsePosition]);
				uT.add(parts[UniversalPosition]);
			}
			if(!words.isEmpty())
			{first = writeOut(fine, coarse, uni, first, words, fT, cT,uT);}
			input.close();
		}
		fine.close();
		coarse.close();
		uni.close();
	}
	/**
	 * @param fine
	 * @param coarse
	 * @param uni
	 * @param first
	 * @param words
	 * @param fT
	 * @param cT
	 * @param uT
	 * @return
	 * @throws IOException
	 */
	protected static boolean writeOut(BufferedWriter fine,
			BufferedWriter coarse, BufferedWriter uni, boolean first,
			ArrayList<String> words, ArrayList<String> fT,
			ArrayList<String> cT, ArrayList<String> uT) throws IOException
	{
		if(!words.isEmpty())
		{
			if(first)
			{first = false;}
			else
			{
				fine.newLine();
				coarse.newLine();
				uni.newLine();
			}
			writeOutLine(words,fT,cT,uT,fine,coarse,uni);
			words.clear();
			fT.clear();
			cT.clear();
			uT.clear();
		}
		return first;
	}
	/**
	 * 
	 * @param words
	 * @param fT
	 * @param cT
	 * @param uT
	 * @param fine
	 * @param coarse
	 * @param uni
	 * @throws IOException 
	 */
	private static void writeOutLine(ArrayList<String> words, ArrayList<String> fT,
			ArrayList<String> cT, ArrayList<String> uT, BufferedWriter fine,
			BufferedWriter coarse, BufferedWriter uni) throws IOException
	{
		write(words,fT,fine);
		write(words,cT,coarse);
		write(words,uT,uni);
	}
	/**
	 * 
	 * @param words
	 * @param uT
	 * @param uni
	 * @throws IOException 
	 */
	private static void write(ArrayList<String> words, ArrayList<String> uT,
			BufferedWriter uni) throws IOException
	{
		for(int i=0;i<words.size();++i)
		{
			if(i!=0)
			{uni.write(" ");}
			uni.write(words.get(i));
			uni.write("|");
			uni.write(uT.get(i));
		}
	}
}